#!/usr/bin/env python
#coding=utf-8
import sys,os
parent = os.path.dirname
udir = parent(parent(parent(os.path.abspath(__file__))))

sys.path.append(udir)

os.environ['DJANGO_SETTINGS_MODULE'] = 'log.settings'

import urllib2,urllib,time

APP_ID = sys.argv[1]
URL = 'http://%s.appspot.com/import'%APP_ID
print URL

from log.upload.models import UploadLog
log = UploadLog.objects.get(app_id=APP_ID)
SIZE = log.batch_size
#print SIZE

f = open(log.data_file)
f.seek(log.where)

def upload(emails):
  values = {'emails': '\n'.join(emails)}
  req = urllib2.Request(URL, urllib.urlencode(values))
  try:
    response = urllib2.urlopen(req)
    r = response.read()
  except  IOError, e:
    print e
    time.sleep(10)
    return
  else:
    if r != 'ok':
      time.sleep(10)
      return
      
    log.where = f.tell()
    log.total += len(emails)
    log.save()
    
    print emails[0], emails[-1]
    time.sleep(1)
    
emails = []
while True:
  data = f.readline()
  if not data:
    upload(emails)
    break
  
  email = data.strip()  
  if email:emails.append(email)
  if len(emails)%SIZE == 0:
    upload(emails)
    emails = []


